/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ACV;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 * This class extend the standard JPanel to allow for images to be painted to the screen.
 * 
 * @author Calvin
 */
public class ImagePanel extends JPanel implements Printable {
    
    /**
     * BufferedImage to be displayed on panel
     */
    public BufferedImage image = null;

    /**
     * Default constructor that sets image to null
     */
    public ImagePanel() {
        
        this.image = null;
    }
    
    /**
     * Constructor that creates an ImagePanel with the specified BufferedImage
     * @param img - BufferedImage to be displayed
     */
    public ImagePanel(BufferedImage img) {
        if (img != null) {
            this.image.flush();
            this.image = img;
        }
    }
    
    /**
     * Sets the BufferedImage to the specified internal resource
     * @param path - path of resource in "package/folder/file.ext" format
     */
    public void setImageInternal(String path) {
        this.image.flush();
        if (path != null && !path.equals("") ) {
            try {
                URI uri = this.getClass().getClassLoader().getResource(path).toURI();
                File file = new File(uri);
                image = ImageIO.read(file);
            } catch (Exception ex) {
                System.out.println("Error Loading Image");
                ex.printStackTrace();
            }
        }
    }
    
    /**
     * Sets the BufferedImage to the specified external resource
     * @param path - path of external resource
     */
    public void setImageExternal(String path) {
        this.image.flush();
        if (path != null && !path.equals("") ) {
            try {
                image = ImageIO.read(new File(path));
            } catch (IOException ex) {
                System.out.println("Error Loading Image");
                ex.printStackTrace();
            }
        }
    }
    
    /**
     * Sets image to that of a predefined BufferedImage
     * @param img - Already initialized BufferedImage
     */
    public void setImage(BufferedImage img) {
        if (img != null) {
            this.image.flush();
            this.image = img;
        }
    }
    

    public void scaleImage(int width, int height) {
                
        this.setBounds(0, 0, width, height);
        this.repaint();
        
//        // Create new (blank) image of required (scaled) size
//
//        BufferedImage scaledImage = new BufferedImage(
//                width, height, BufferedImage.TYPE_INT_ARGB);
//        // Paint scaled version of image to new image
//        Graphics2D graphics2D = scaledImage.createGraphics();
//        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//        graphics2D.drawImage(this.image, 0, 0, width, height, null);
//        
//        this.image.flush();
//        this.image = scaledImage;
//        //scaledImage.flush();
//        
//        // clean up
//        graphics2D.dispose();

    }
    
    public int print( Graphics g , PageFormat pageFormat , int pi )   
    {  
        if ( pi >= 1 )   
        {  
            return Printable.NO_SUCH_PAGE;  
        }  
        paintComponent( g );  
        return Printable.PAGE_EXISTS;  
    }  

    /**
     * Overrides paintComponent() and checks if image is null before drawing 
     * @param g - 
     */
    @Override
    public void paintComponent(Graphics g) {
        if(this.image != null) {
            //g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters
            g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
        }
    }

    
}
